﻿using System;

namespace Gmantis.Controls.DataGrid
{
    public class DataGridCell
    {
        // Methods
        internal DataGridCell(DataGridRow row, DataGridColumn column)
        {
            if (((row.DataGrid == null) || (column.DataGrid == null)) || (row.DataGrid != column.DataGrid))
            {
                throw new ArgumentException("Both column and row must belong to the same data grid.");
            }
            this.Row = row;
            this.Column = column;
        }

        public override bool Equals(object obj)
        {
            return (this == (obj as DataGridCell));
        }

        public override int GetHashCode()
        {
            return (this.Row.GetHashCode() ^ this.Column.GetHashCode());
        }

        public static bool operator ==(DataGridCell obj, DataGridCell obj2)
        {
            if ((obj == null) && (obj2 == null))
            {
                return true;
            }
            if ((obj == null) || (obj2 == null))
            {
                return false;
            }
            return ((obj2.Column == obj.Column) && (obj2.Row == obj.Row));
        }

        public static bool operator !=(DataGridCell obj, DataGridCell obj2)
        {
            return !(obj == obj2);
        }

        public override string ToString()
        {
            return string.Format("(Row = {0}, Column = {1})", this.Row, this.Column);
        }

        // Properties
        public DataGridColumn Column { get; private set; }

        public DataGrid DataGrid
        {
            get
            {
                if (this.Row.DataGrid == this.Column.DataGrid)
                {
                    return this.Row.DataGrid;
                }
                return null;
            }
        }

        public bool IsEditable
        {
            get
            {
                return (this.Column.ActualIsEditable && this.Row.IsEditable);
            }
        }

        public bool IsEditing
        {
            get
            {
                DataGridCell cell = (this.DataGrid != null) ? this.DataGrid.Model.FocusAndEditingManager.CurrentEditingCell : null;
                return (((cell != null) && (cell.Row == this.Row)) && (cell.Column == this.Column));
            }
        }

        public bool IsMouseOver
        {
            get
            {
                return ((this.DataGrid != null) && this.DataGrid.View.IsMouseOverCell(this.Row, this.Column));
            }
        }

        public bool IsSelectable
        {
            get
            {
                return (((this.DataGrid != null) && this.Column.IsSelectable) && this.Row.IsSelectable);
            }
        }

        public bool IsSelected
        {
            get
            {
                return ((this.DataGrid != null) && this.DataGrid.Model.SelectionManager.SelectedCells.Contains(this));
            }
        }

        public DataGridCellPresenter Presenter
        {
            get
            {
                if ((this.DataGrid != null) && (this.DataGrid.View != null))
                {
                    return this.DataGrid.View.GetCellPresenter(this.Row, this.Column);
                }
                return null;
            }
        }

        public DataGridRow Row { get; private set; }

        public string Text
        {
            get
            {
                return this.Row.GetCellText(this.Column);
            }
        }

        public object Value
        {
            get
            {
                return this.Row.GetCellValue(this.Column);
            }
            set
            {
                this.Row.SetCellValue(this.Column, value);
                if ((this.DataGrid != null) && (this.DataGrid.View != null))
                {
                    this.DataGrid.View.OnCellValueUpdated(this);
                }
            }
        }
    }
}
